package com.collect.zambia.zhiScene.zhiRemind.zhiCustomer.zhiAdmin.zhiPerformance;

import android.annotation.SuppressLint;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.collect.zambia.R;
import com.collect.zambia.zhiAdater.zhiLevelDetail.ZHILevelDetailCustomerAdmin;
import com.collect.zambia.zhiBase.BaseActivity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRCustomerPerformanceAdminListEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRCustomerPerformanceEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRCustomerPerformanceListEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISPerfermanceEntity;
import com.collect.zambia.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.zambia.zhiScene.zhiRemind.zhiCustomer.zhiMember.zhiPerformance.zhiSort.ZHICustomerPerformanceCompare;
import com.collect.zambia.zhiScene.zhiWidget.zhiDialog.ZHIDialogCustomerPerformanceMember;
import com.collect.zambia.zhiScene.zhiWidget.zhiDialog.ZHIDialogMessage;
import com.collect.zambia.zhiUtils.StringUtil;
import com.collect.zambia.zhiUtils.ZHICalendarUtil;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_PERFORMANCE_CUSTOMER_OLD_ADMIN)
public class ZHICustomerPerformanceAdminOldActivity extends BaseActivity<ZHICustomerPerformanceAdminPersenter> implements ZHICustomerPerformanceAdminContract.View {

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.tv_page_title)
    TextView tv_page_title;

    @BindView(R.id.tv_cumulative_bonus_unit)
    TextView tv_cumulative_bonus_unit;

    @BindView(R.id.tv_cumulative_bonus)
    TextView tv_cumulative_bonus;

    @BindView(R.id.tv_cumulative_bonus_date)
    TextView tv_cumulative_bonus_date;
    @BindView(R.id.iv_cumulative_bonus_date)
    ImageView iv_cumulative_bonus_date;

    @BindView(R.id.tv_total_orders_num)
    TextView tv_total_orders_num;
    @BindView(R.id.tv_total_collected_num)
    TextView tv_total_collected_num;
    @BindView(R.id.tv_total_collected_rate)
    TextView tv_total_collected_rate;

    @BindView(R.id.tv_case_total_num)
    TextView tv_case_total_num;
    @BindView(R.id.tv_case_settle_num)
    TextView tv_case_settle_num;
    @BindView(R.id.tv_case_collected_rate)
    TextView tv_case_collected_rate;

    @BindView(R.id.iv_note)
    ImageView iv_note;
    @BindView(R.id.iv_filter)
    ImageView iv_filter;

    @BindView(R.id.rc_detail_list)
    RecyclerView rc_detail_list;

    @BindView(R.id.iv_sort)
    ImageView iv_sort;

    @BindView(R.id.ll_dis_date_bg)
    LinearLayout ll_dis_date_bg;

    @BindView(R.id.ll_user_bg)
    LinearLayout ll_user_bg;

    @BindView(R.id.ll_num_bg)
    LinearLayout ll_num_bg;

    @BindView(R.id.ll_settle_num_bg)
    LinearLayout ll_settle_num_bg;

    @BindView(R.id.ll_collected_rate_bg)
    LinearLayout ll_collected_rate_bg;

    @BindView(R.id.rc_detail_list_2)
    RecyclerView rc_detail_list_2;

    @BindView(R.id.iv_sort_2)
    ImageView iv_sort_2;

    @BindView(R.id.ll_dis_date_bg_2)
    LinearLayout ll_dis_date_bg_2;

    @BindView(R.id.ll_user_bg_2)
    LinearLayout ll_user_bg_2;

    @BindView(R.id.ll_num_bg_2)
    LinearLayout ll_num_bg_2;

    @BindView(R.id.ll_settle_num_bg_2)
    LinearLayout ll_settle_num_bg_2;

    @BindView(R.id.ll_collected_rate_bg_2)
    LinearLayout ll_collected_rate_bg_2;

    private boolean mIsSortDisDateDes = true;
    private boolean mIsSortRepayDateDes = false;
    private boolean mIsSortNumDes = false;
    private boolean mIsSortSettleNumDes = false;
    private boolean mIsSortCollectedRateDes = false;

    private boolean mIsSortCollectedRateDes2 = false;

    private final ZHILevelDetailCustomerAdmin mZHILevelDetailCustomerAdmin = new ZHILevelDetailCustomerAdmin();
    private final ZHILevelDetailCustomerAdmin mZHILevelDetailCustomerAdmin2 = new ZHILevelDetailCustomerAdmin();

    private ArrayList<ZHIRCustomerPerformanceEntity> mCustomerPerformanceEntityArrayList;
    private ArrayList<ZHIRCustomerPerformanceEntity> mCustomerPerformanceEntityArrayList2;

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_perfermance_customer_admin_old;
    }

    @Override
    protected void initView() {
        try {
            ZHICalendarUtil.initDateTimeOf0706L();
            ZHICalendarUtil.initDateTimeOf0801L();

            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

            tv_page_title.setOnClickListener(v -> {
            });

            iv_note.setOnClickListener(v -> finish());

            iv_sort.setOnClickListener(v -> {
//                try {
//                    if (mCustomerPerformanceEntityArrayList == null) {
//                        return;
//                    }
//
//                    if (mIsSortCollectedRateDes) {
//                        mIsSortCollectedRateDes = false;
//                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareBySettleRateASC());
//                    } else {
//                        mIsSortCollectedRateDes = true;
//                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareBySettleRateDESC());
//                    }
//
//                    mZHILevelDetailCustomerAdmin.notifyDataSetChanged();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            });

            ll_dis_date_bg.setOnClickListener(v -> {
//                try {
//                    if (mCustomerPerformanceEntityArrayList == null) {
//                        return;
//                    }
//
//                    if (mIsSortDisDateDes) {
//                        mIsSortDisDateDes = false;
//                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareByAssignDateASC());
//                    } else {
//                        mIsSortDisDateDes = true;
//                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareByAssignDateDESC());
//                    }
//
//                    mZHILevelDetailCustomerAdmin.notifyDataSetChanged();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            });

            ll_user_bg.setOnClickListener(v -> {
//                try {
//                    if (mCustomerPerformanceEntityArrayList == null) {
//                        return;
//                    }
//
//                    if (mIsSortRepayDateDes) {
//                        mIsSortRepayDateDes = false;
//                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareByRepayDateASC());
//                    } else {
//                        mIsSortRepayDateDes = true;
//                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareByRepayDateDESC());
//                    }
//
//                    mZHILevelDetailCustomerAdmin.notifyDataSetChanged();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            });

            ll_num_bg.setOnClickListener(v -> {
//                try {
//                    if (mCustomerPerformanceEntityArrayList == null) {
//                        return;
//                    }
//
//                    if (mIsSortNumDes) {
//                        mIsSortNumDes = false;
//                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareByAssignNumASC());
//                    } else {
//                        mIsSortNumDes = true;
//                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareByAssignNumDESC());
//                    }
//
//                    mZHILevelDetailCustomerAdmin.notifyDataSetChanged();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            });

            ll_settle_num_bg.setOnClickListener(v -> {
//                try {
//                    if (mCustomerPerformanceEntityArrayList == null) {
//                        return;
//                    }
//
//                    if (mIsSortSettleNumDes) {
//                        mIsSortSettleNumDes = false;
//                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareBySettleNumASC());
//                    } else {
//                        mIsSortSettleNumDes = true;
//                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareBySettleNumDESC());
//                    }
//
//                    mZHILevelDetailCustomerAdmin.notifyDataSetChanged();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            });

            ll_collected_rate_bg.setOnClickListener(v -> {
                try {
                    if (mCustomerPerformanceEntityArrayList == null) {
                        return;
                    }

                    if (mIsSortCollectedRateDes) {
                        mIsSortCollectedRateDes = false;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareBySettleRateASC());
                    } else {
                        mIsSortCollectedRateDes = true;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareBySettleRateDESC());
                    }

                    mZHILevelDetailCustomerAdmin.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            ll_collected_rate_bg_2.setOnClickListener(v -> {
                try {
                    if (mCustomerPerformanceEntityArrayList2 == null) {
                        return;
                    }

                    if (mIsSortCollectedRateDes2) {
                        mIsSortCollectedRateDes2 = false;
                        Collections.sort(mCustomerPerformanceEntityArrayList2, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareBySettleRateASC());
                    } else {
                        mIsSortCollectedRateDes2 = true;
                        Collections.sort(mCustomerPerformanceEntityArrayList2, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareBySettleRateDESC());
                    }

                    mZHILevelDetailCustomerAdmin2.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            iv_filter.setOnClickListener(v -> {

            });

            iv_cumulative_bonus_date.setOnClickListener(v -> ZHIProjectRouter.toPerformanceCustomerHistoryAdmin());
            tv_cumulative_bonus_date.setOnClickListener(v -> ZHIProjectRouter.toPerformanceCustomerHistoryAdmin());

            initEvent();
            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @Override
    public void onGetCustomerFollowDLeaderPerformance(ZHIRCustomerPerformanceAdminListEntity zhirCustomerPerformanceAdminListEntity, ZHISPerfermanceEntity zhisPerfermanceEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            ///////////////////////
            ArrayList<ZHIRCustomerPerformanceEntity> zhirCustomerPerformanceEntityArrayList = zhirCustomerPerformanceAdminListEntity.getPart1List();

            ///////////////////////
            tv_cumulative_bonus.setText(zhirCustomerPerformanceAdminListEntity.getBonusFormat());

            ///////////////////////
            int totalNum = 0;
            for (int i = 0; i < zhirCustomerPerformanceEntityArrayList.size(); i++) {
                ZHIRCustomerPerformanceEntity zhirCustomerPerformanceEntity = zhirCustomerPerformanceEntityArrayList.get(i);
                zhirCustomerPerformanceEntity.setHighlightColor(false);
                totalNum += zhirCustomerPerformanceEntity.getAssignNumFormatInt();
            }
            tv_case_total_num.setText(String.valueOf(totalNum));

            ///////////////////////
            int settledNum = 0;
            for (int i = 0; i < zhirCustomerPerformanceEntityArrayList.size(); i++) {
                ZHIRCustomerPerformanceEntity zhirCustomerPerformanceEntity = zhirCustomerPerformanceEntityArrayList.get(i);
                zhirCustomerPerformanceEntity.setHighlightColor(false);
                settledNum += zhirCustomerPerformanceEntity.getRepayNumFormatInt();
            }
            tv_case_settle_num.setText(String.valueOf(settledNum));

            ///////////////////////
            String collectRateStr = "0";
            if (totalNum > 0) {
                float totalNumL = totalNum * 1.0F;
                float settledNumL = settledNum * 1.0F;
                float collectRateL = settledNumL / totalNumL;
                collectRateStr = StringUtil.numberFormatterThousandPercent(collectRateL + "");
            }
            tv_case_collected_rate.setText(collectRateStr);

            ///////////////////////
//            Collections.sort(zhirCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareByAssignDateDESC());
            mCustomerPerformanceEntityArrayList = zhirCustomerPerformanceEntityArrayList;
            mZHILevelDetailCustomerAdmin.setNewData(zhirCustomerPerformanceEntityArrayList);


            ///////////////////////-------part2-----------
            ArrayList<ZHIRCustomerPerformanceEntity> zhirCustomerPerformanceEntityArrayList2 = zhirCustomerPerformanceAdminListEntity.getPart2List();

            ///////////////////////
            int totalNum2 = 0;
            for (int i = 0; i < zhirCustomerPerformanceEntityArrayList2.size(); i++) {
                ZHIRCustomerPerformanceEntity zhirCustomerPerformanceEntity = zhirCustomerPerformanceEntityArrayList2.get(i);
                zhirCustomerPerformanceEntity.setHighlightColor(true);
                totalNum2 += zhirCustomerPerformanceEntity.getAssignNumFormatInt();
            }
            tv_total_orders_num.setText(String.valueOf(totalNum2));

            ///////////////////////
            int settledNum2 = 0;
            for (int i = 0; i < zhirCustomerPerformanceEntityArrayList2.size(); i++) {
                ZHIRCustomerPerformanceEntity zhirCustomerPerformanceEntity = zhirCustomerPerformanceEntityArrayList2.get(i);
                zhirCustomerPerformanceEntity.setHighlightColor(true);
                settledNum2 += zhirCustomerPerformanceEntity.getRepayNumFormatInt();
            }
            tv_total_collected_num.setText(String.valueOf(settledNum2));

            ///////////////////////
            String collectRateStr2 = "0";
            if (totalNum2 > 0) {
                float totalNumL2 = totalNum2 * 1.0F;
                float settledNumL2 = settledNum2 * 1.0F;
                float collectRateL2 = settledNumL2 / totalNumL2;
                collectRateStr2 = StringUtil.numberFormatterThousandPercent(collectRateL2 + "");
            }
            tv_total_collected_rate.setText(collectRateStr2);

            ///////////////////////
//            Collections.sort(zhirCustomerPerformanceEntityArrayList2, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareByAssignDateDESC());
            mCustomerPerformanceEntityArrayList2 = zhirCustomerPerformanceEntityArrayList2;
            mZHILevelDetailCustomerAdmin2.setNewData(zhirCustomerPerformanceEntityArrayList2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetCustomerFollowDPerformance(ZHIRCustomerPerformanceListEntity zhirCustomerPerformanceListEntity, ZHISPerfermanceEntity zhisPerfermanceEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            ///////////////////////
            ArrayList<ZHIRCustomerPerformanceEntity> zhirCustomerPerformanceEntityArrayListOri = zhirCustomerPerformanceListEntity.getDataList();
            ArrayList<ZHIRCustomerPerformanceEntity> zhirCustomerPerformanceEntityArrayList = new ArrayList<>();

            for (int i = 0; i < zhirCustomerPerformanceEntityArrayListOri.size(); i++) {
                ZHIRCustomerPerformanceEntity zhirCustomerPerformanceEntity = zhirCustomerPerformanceEntityArrayListOri.get(i);
                zhirCustomerPerformanceEntity.setLimitDate(zhirCustomerPerformanceListEntity.getLimitDate());
                if (zhirCustomerPerformanceEntity.isAfter0706Date()) {
                    zhirCustomerPerformanceEntityArrayList.add(zhirCustomerPerformanceEntity);
                }
            }

            ///////////////////////
            ZHIDialogCustomerPerformanceMember.showDataSource(getActivity(), zhisPerfermanceEntity.getSysLoginName(), zhirCustomerPerformanceEntityArrayList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint({"InflateParams", "SetTextI18n"})
    public void initEvent() {
        srl_swipe_layout.setOnRefreshListener(() -> initData(false));

        mZHILevelDetailCustomerAdmin.setEmptyView(getRefreshEmptyView());
        mZHILevelDetailCustomerAdmin.setOnItemClickListener((adapter, view, position) -> {
            if (mCustomerPerformanceEntityArrayList != null && mCustomerPerformanceEntityArrayList.size() > position) {
                ZHIRCustomerPerformanceEntity zhirCustomerPerformanceEntity = mCustomerPerformanceEntityArrayList.get(position);
                ZHISPerfermanceEntity zhisPerfermanceEntity = getUserCaseSearchParams(zhirCustomerPerformanceEntity);
                assert mPresenter != null;
                mPresenter.getCustomerFollowDPerformance(getActivity(), zhisPerfermanceEntity, true);
            }
        });

        rc_detail_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
        rc_detail_list.setAdapter(mZHILevelDetailCustomerAdmin);

        /////////////////////
        mZHILevelDetailCustomerAdmin2.setEmptyView(getRefreshEmptyView());
        mZHILevelDetailCustomerAdmin2.setOnItemClickListener((adapter, view, position) -> {
            if (mCustomerPerformanceEntityArrayList2 != null && mCustomerPerformanceEntityArrayList2.size() > position) {
                ZHIRCustomerPerformanceEntity zhirCustomerPerformanceEntity = mCustomerPerformanceEntityArrayList2.get(position);
                ZHISPerfermanceEntity zhisPerfermanceEntity = getUserCaseSearchParams(zhirCustomerPerformanceEntity);
                assert mPresenter != null;
                mPresenter.getCustomerFollowDPerformance(getActivity(), zhisPerfermanceEntity, true);
            }
        });

        rc_detail_list_2.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
        rc_detail_list_2.setAdapter(mZHILevelDetailCustomerAdmin2);
    }

    public void initData(boolean showDialog) {
        ZHISPerfermanceEntity zhisPerfermanceEntity = getCaseSearchParams();
        assert mPresenter != null;
        mPresenter.getCustomerFollowDLeaderPerformance(getActivity(), zhisPerfermanceEntity, showDialog);
    }

    public void refreshData() {
        initData(true);
    }

    private ZHISPerfermanceEntity getCaseSearchParams() {

//        String gStartDate = ZHICalendarUtil.getCurMonthFirstDayFormatString();
//        String gEndDate = ZHICalendarUtil.getCurMonthCurDayFormatString();
//
//        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
//        zhisPerfermanceEntity.setLeven(ZHIMainProcessHelper.getInstance().getLoginEntity().getDefaultLevenApiFormat());
//        zhisPerfermanceEntity.setStartDateForDisplay(gStartDate);
//        zhisPerfermanceEntity.setEndDateForDisplay(gEndDate);
//        zhisPerfermanceEntity.setStartDate(gStartDate);
//        zhisPerfermanceEntity.setEndDate(gEndDate);
//        zhisPerfermanceEntity.setLeaderUserId(ZHIMainProcessHelper.getInstance().getLoginEntity().getUserId());
        return new ZHISPerfermanceEntity();
    }

    private ZHISPerfermanceEntity getUserCaseSearchParams(ZHIRCustomerPerformanceEntity zhirCustomerPerformanceEntity) {
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
        zhisPerfermanceEntity.setSysLoginName(zhirCustomerPerformanceEntity.getSysLoginName());
        return zhisPerfermanceEntity;
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> initData(true));
        return emptyView;
    }

    private void showBonusDialog() {
        ZHIDialogMessage.showDialog(getActivity(), null, new SpannableStringBuilder(""), true, null);
    }
}
